﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using Website.Logger;
using DAL;
using BOL;
using WebLib;
using System.Web.Script.Services;
using System.Web.Services;


namespace UILGestion.Admin.Empresas
{
    public partial class EmpresaInsertForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                try
                {

                    //idObra.Value = Request.Params["idObra"];
                    menuCtrl1.loadMenuSeguridad();
                    FormatForm();

                }
                catch (SqlException sqlex)
                {
                    WebServerSideSqlError.Log(sqlex);

                    ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                     "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


                }
                catch (Exception ex)
                {
                    WebServerSideError.Log(ex);
                    ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                         "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

                }

            }
        }
        protected void btnContinue_OnClick(object sender, EventArgs e)
        {
            saveButton.Enabled = false;
            try
            {

                if ((sender as Button).Text == "Guardar")
                {

                    EmpresaBL empresaBL = new EmpresaBL();

                    Empresa empresa = PopulateObjectInsert();
                    int idEmpresa=empresaBL.add(empresa);

                    internalid.Value = idEmpresa.ToString();
                    Response.Redirect("EmpresaReadForm.aspx?id=" + internalid.Value, false);
                }
                if ((sender as Button).Text == "Cancelar")
                {
                    Response.Redirect("EmpresaListForm.aspx");
                }
                if ((sender as Button).Text == "refreshButton")
                {
                    FormatForm();
                }


            }
            catch (SqlException sqlex)
            {
                WebServerSideSqlError.Log(sqlex);

                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                 "uniqueKey" + DateTime.Now, "alert('" + sqlex.Message.Replace("'", "") + "');", true);


            }
            catch (Exception ex)
            {
                WebServerSideError.Log(ex);
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page),
                     "uniqueKey" + DateTime.Now, "alert('" + ex.Message.Replace("'", "") + "');", true);

            }




        }
        private void ShowAllComboBox()
        {
            //dbGestionDataContext _db = new dbGestionDataContext(Parametros.strConnString);
            //var q = from i in _db.FormaPago
            //        select i;


            //formaPagoDropDownList.DataTextField = "descripcion";
            //formaPagoDropDownList.DataValueField = "idInternal";

            //formaPagoDropDownList.DataSource = q;
            //formaPagoDropDownList.DataBind();


        }

        private Empresa PopulateObjectInsert()
        {
            //(DD/MM/yyyy)
            System.IFormatProvider MiFp =
                new System.Globalization.CultureInfo("es-ES", false);


            Empresa entity = new Empresa();

           
            entity.descripcion = presupuestoTextBox.Text;
            entity.direccion = direccionTextBox.Text;
            entity.activo=ActivoCheckBox.Checked;
            entity.ruc=rucTextBox.Text;
            entity.razonSocial = razonSocialTextBox.Text;
            entity.usuarioCre = (int)Session["idEmpleado"];
            //int idpadre = 0;
            //  int.TryParse(idPadreTextBox.Text.ToString(),out   idpadre);
            return entity;
        }
        private void newObject()
        {

            //correlativo
            dbGestionDataContext _db = new dbGestionDataContext(Parametros.strConnString);
            Empresa empresa = new Empresa();
            codigoLabel.Text = empresa.getCorrelativo(_db);

        }

        private void FormatForm()
        {

            ShowAllComboBox();
            newObject();




        }

      
    }
}